package com.onlinelibrary.activities.login;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.databinding.BindingAdapter;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.onlinelibrary.activities.App;
import com.onlinelibrary.activities.sign.SignActivity;
import com.onlinelibrary.utils.Http;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class LoginVM extends AndroidViewModel {
    @SuppressLint("StaticFieldLeak")
    private static LoginActivity loginActivity;

    public static MutableLiveData<String>email=new MutableLiveData<>(),password=new MutableLiveData<>();
    public static MutableLiveData<Boolean>autoLogin=new MutableLiveData<>();

    public LoginVM(@NonNull Application application) {
        super(application);
        autoLogin.setValue(true);
        email.setValue("");
        password.setValue("");
    }
    public void setBinding(LoginActivity loginActivity) {
        LoginVM.loginActivity=loginActivity;
        loginActivity.setResult(2);
    }
    public static void login(View view){
        if (!email.getValue().equals("")&&!password.getValue().equals("")){
            Http.get("user/verify/?email="+email.getValue()+"&password="+password.getValue(), new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) { }
                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) {
                   loginActivity.runOnUiThread(() -> {
                       try {
                           assert response.body() != null;
                           String res = response.body().string();
                           @SuppressLint("CommitPrefEdits") SharedPreferences.Editor edit = App.sp.edit();
                           if (res.equals("验证成功")){
                               edit.putString("account",email.getValue());
                               edit.putString("password",password.getValue());
                               edit.putBoolean("autoLogin", autoLogin.getValue());
                               loginActivity.setResult(3);
                               loginActivity.finish();
                           }else {
                               edit.putString("account","");
                               edit.putString("password","");
                               edit.putBoolean("autoLogin",false);
                           }
                           edit.apply();
                           Toast.makeText(loginActivity,res, Toast.LENGTH_SHORT).show();
                       } catch (IOException e) {
                           e.printStackTrace();
                       }
                   });
                }
            });
        }else {
            Toast.makeText(loginActivity, "清填写完整", Toast.LENGTH_SHORT).show();
        }
    }
    public static void sign(View view){
        loginActivity.startActivity(new Intent(loginActivity, SignActivity.class));
    }
}
